package com.tanhua.controller;


import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.UserInfo;
import com.tanhua.server.UserInfoService;
import com.tanhua.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    /*
    *
    * 查询用户资料
    *
    * */
    @GetMapping
    public ResponseEntity users(Long userID){
       /* boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return  ResponseEntity.status(401).body(null);
        }*/
       /* Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
       //获取ThreadLocal线程中的userId，通过userId查询UserInfo表中的数据，前端通过数据的双向绑定，直接展示数据
        if(userID ==null){
            userID = UserHolder.getUserId();
        }
       UserInfoVo userInfoVo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }
    /*
    * 修改userInfo的基本信息
    * */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
       /* boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return  ResponseEntity.status(401).body(null);
        }*/
       /* Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/

        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);

    }
}
